package fina2.dcs.fi;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Synchronized;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import fina2.dcs.common.UIComponentHelper;
import fina2.dcs.context.ContextHelper;
import fina2.dcs.fis.LicenceType;
import fina2.dcs.fis.LicenceTypeLocal;

@Name("fiAddLicense")
@Scope(ScopeType.SESSION)
@Synchronized(timeout = 5000)
public class FiListAddLicense implements Serializable{
	private List<SelectItem> licenses = null;
	private String selectedLicense = null;
	private String code;
	private Date licenseDate;
	private Date changeDate;
	private String reason;
	private boolean operational;

	private InitialContext ic = null;
	private LicenceTypeLocal licLocal = null;

	private Log log = Logging.getLog(getClass());
	
	public FiListAddLicense() {
		try {
			ic = ContextHelper.getInitialContext();
			licLocal = ContextHelper.injectLicenceTypeLocal(ic);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public List<SelectItem> getLicenses() {
		licenses = new ArrayList<SelectItem>();
		licLocal.init();
		List<LicenceType> lics = licLocal.getLicences();
		for (int i = 0; i < lics.size(); i++) {
			licenses.add(new SelectItem(lics.get(i).getDescription()));
		}
		return licenses;
	}

	public void setLicenses(List<SelectItem> licenses) {
		this.licenses = licenses;
	}

	public String getSelectedLicense() {
		return selectedLicense;
	}

	public void setSelectedLicense(String selectedLicense) {
		this.selectedLicense = selectedLicense;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Date getLicenseDate() {
		return licenseDate;
	}

	public void setLicenseDate(Date licenseDate) {
		this.licenseDate = licenseDate;
	}

	public Date getChangeDate() {
		return changeDate;
	}

	public void setChangeDate(Date changeDate) {
		this.changeDate = changeDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public boolean isOperational() {
		return operational;
	}

	public void setOperational(boolean operational) {
		this.operational = operational;
	}

	public void flushPanel() {
		UIComponentHelper.clearComponent(FacesContext.getCurrentInstance().getViewRoot());
		code = null;
		licenseDate = null;
		changeDate = null;
		reason = null;
		operational = false;
	}
}
